import {ref} from 'vue';
import {defineStore} from 'pinia';

export const useDictStore = defineStore('dict', () => {
  const dict = ref<any[]>([]);
  // 获取字典
  const getDict = (itemKey: string) => {
    if (itemKey == null && itemKey == '') {
      return null;
    }
    try {
      for (let i = 0; i < dict.value.length; i++) {
        if (dict.value[i].key == itemKey) {
          return dict.value[i].value;
        }
      }
    } catch (error) {
      return null;
    }
  };

  // 设置字典
  const setDict = (itemKey: string, itemValue: string | number) => {
    if (itemKey !== null && itemKey !== '') {
      dict.value.push({
        key: itemKey,
        value: itemValue,
      });
    }
  };

  // 删除字典
  const removeDict = (itemKey: string) => {
    let bln = false;
    try {
      for (let i = 0; i < dict.value.length; i++) {
        if (dict.value[i].key === itemKey) {
          dict.value.splice(i, 1);
          return true;
        }
      }
    } catch (error) {
      bln = false;
    }
    return bln;
  };

  // 清空字典
  const cleanDict = () => {
    dict.value = [];
  };

  // 初始字典
  const initDict = () => {};
  return {getDict, setDict, removeDict, cleanDict, initDict};
});
